package com.itheima.health.controller;



import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constants.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    MenuService menuService;

    @Reference
    UserService userService;

    @RequestMapping("/getMenu")
    public Result getMenu() {

        try{
            User user =
                    (User)
                            SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            com.itheima.health.pojo.User user1 = userService.findUserByUsername(user.getUsername());

            Set<Role> roles = user1.getRoles();

            List<Menu> menulist = new ArrayList<>();
            for (Role role : roles) {
                        Integer id = role.getId();
                        List<Menu> list1 = menuService.findMenu(id);
                        if(list1.size() > menulist.size()) {
                            menulist = list1;
                }
            }

            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menulist);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }

    }

    @RequestMapping("/selectPage")
    public PageResult selectPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = menuService.pageQuery(
                //传递当前页, 每页显示的记录数, 查询条件
                //响应PageResult, 封装总记录数, 结果集
                queryPageBean.getCurrentPage(),
                queryPageBean.getQueryString(),
                queryPageBean.getPageSize()
        );

        return pageResult;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
        } catch (Exception e) {
            return new Result(false, MessageConstant.FAIL);
        }

        return new Result(true, MessageConstant.SUCCESS);

    }

    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu) {
        try {
            menuService.update(menu);
        } catch (Exception e) {
            return new Result(false, MessageConstant.FAIL);
        }

        return new Result(true, MessageConstant.SUCCESS);

    }

    @RequestMapping("/selectById")
    public Result selectById(Integer id) {
        Menu menu = menuService.findById(id);
        if(menu != null){
            Result result = new Result(true, MessageConstant.SUCCESS,menu);
            return result;
        }
        return new Result(false,MessageConstant.FAIL);
    }

    @RequestMapping("/selectAll")
    public List<Menu> selectAll(Integer id) {
        List<Menu> list = menuService.selectAll(id);

        return list;
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            menuService.delete(id);
            return new Result(true, MessageConstant.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }

}
